package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.controller.dto.ProgramFile;
import com.example.demo.entity.HmProgram;
import com.example.demo.mapper.ProgramMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/program")
public class ProgramController extends BaseController {

    @Resource
    ProgramMapper programMapper;

    @PostMapping
    public Result<?> save(@RequestBody HmProgram request) {
        if(1==request.getPlayType()){
            programMapper.deleteByDeviceId(request.getDeviceId());
        }else{

        }

        ProgramFile[] fileList = request.getFileList();
        for(ProgramFile file : fileList){
            String url = file.getResponse().getData().getUrl();
            HmProgram hmProgram = new HmProgram();
            hmProgram.setDeviceId(request.getDeviceId());
            hmProgram.setName(file.getName());
            hmProgram.setUrl(url);
            programMapper.insert(hmProgram);
        }

        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody HmProgram hmProgram) {
        programMapper.updateById(hmProgram);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        programMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(programMapper.selectById(id));
    }

    /**
     *
     * @author fqc
     * @date 2023/11/23 17:43
     * @param pageNum
     * @param pageSize
     * @param deviceId
     * @param name
     * @return com.example.demo.common.Result<?>
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") Integer deviceId,
                              @RequestParam(defaultValue = "") String name) {
        LambdaQueryWrapper<HmProgram> wrapper = Wrappers.<HmProgram>lambdaQuery();
        if (null!=deviceId && !"".equals(deviceId)) {
            wrapper.like(HmProgram::getDeviceId, deviceId);
        }
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(HmProgram::getName, name);
        }
        wrapper.orderByDesc(HmProgram::getDeviceId);
        Page<HmProgram> programPage = programMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(programPage);
    }

    @GetMapping("/get")
    public Result<?> findByDevice(@RequestParam(defaultValue = "") Integer deviceId) {
        LambdaQueryWrapper<HmProgram> wrapper = Wrappers.<HmProgram>lambdaQuery();
        if (null!=deviceId && !"".equals(deviceId)) {
            wrapper.eq(HmProgram::getDeviceId, deviceId);
        }
        return Result.success(programMapper.selectList(wrapper));
    }

}
